package com.client.controller;

import com.alibaba.fastjson.JSONObject;
import com.client.entity.sm9.Key;
import com.client.mapper.MessageDao;
import com.client.service.Certification;
import com.client.service.KeyService;
import com.client.service.MessageService;
import com.client.service.Sm9Service;
import com.client.utils.MessageUtils;
import com.client.utils.UDPUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Slf4j
@RestController
public class UserController {
    @Autowired
    Sm9Service sm9Service;
    @Autowired
    KeyService keyService;
    @Autowired
    Certification certification;
    @Autowired
    UDPUtil udp;
    @Autowired
    MessageService messageService;

    //签名
    @PostMapping("/sign")
    public Map<String, String> sign(@RequestBody JSONObject data) {
        String msg = data.getString("msg");
        Map<String, String> sign = null;
        try {
            sign = sm9Service.Sign(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sign;
    }

    //验签
    @PostMapping("/verify")
    public boolean verify(@RequestBody JSONObject data) {
        String msg = data.getString("msg");
        String h = data.getString("h");
        String s = data.getString("s");
        String id = data.getString("id");
        boolean flag = false;
        try {
            flag = sm9Service.Verify(id, s, h, msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    //加密
    @PostMapping("/sm9encrypt")
    public Map<String, String> encrypt(@RequestBody JSONObject data) {
        String msg = data.getString("msg");
        String id = data.getString("id");
        String m = null;
        Map<String, String> map = new HashMap<>();
        try {
            m = sm9Service.Encrypt(id, msg);
            map.put("msg", m);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    //解密
    @PostMapping("/decrypt")
    public Map<String, String> decrypt(@RequestBody JSONObject data) {
        String msg = data.getString("msg");
        String m = null;
        Map<String, String> map = new HashMap<>();
        try {
            m = sm9Service.Decrypt(msg);
            map.put("msg", m);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    @Autowired
    MessageDao messageDao;

    //发送认证请求1
    @PostMapping("/udpsend")
    public String udpsend(@RequestBody JSONObject data) {
        String goal_id = data.getString("id");
        String goal_ip = data.getString("ip");
        String goal_port = data.getString("port");
        try {
            UUID uid = MessageUtils.getuuid();
            byte[] bytes = certification.send1(goal_id, uid);

            messageService.newClient(uid, goal_id, Integer.parseInt(goal_port), goal_ip);
            messageService.getClient(goal_id).setTime(MessageUtils.getNowTime());
            udp.send(goal_ip, Integer.parseInt(goal_port), bytes);
            MessageService.addMessage(uid.toString(), goal_ip, Integer.valueOf(goal_port), bytes, MessageUtils.getNowTime(), 8, 30);
            messageDao.add(goal_id, keyService.getId(), goal_ip, Integer.parseInt(goal_port), uid.toString(), MessageUtils.getuuid().toString(), 1, "认证1", new Timestamp(MessageUtils.getNowTime().getTime()));

//            log.info("与[{}]的会话id为[{}]", goal_id, uid.toString());
            return "udp发送成功";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "udp发送失败";
    }

    //从KGC获取密钥，并保存
    @PostMapping("/getsavekey")
    public String getsavekey(@RequestBody JSONObject data) {
        try {
            Key key = keyService.getKeyFormKGC(keyService.getId());
            keyService.saveKey(key);
            return "密钥请求且保存成功";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "密钥请求失败";
    }


}
